local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")


local ChangeHeadLayer = class("ChangeHeadLayer",function(scene)
    local headlayer =	display.newLayer(cc.c4b(0, 0, 0, 180)) --display.newLayer()
    return headlayer
end)

function ChangeHeadLayer:ctor(scene)
    ExternalFun.registerNodeEvent(self)
    self._scene = scene

    self:loadCSB()
    self:init()

    self:setContentSize(yl.WIDTH,yl.HEIGHT)

    --CHANGEFACE
    local events = {"CHANGEFACE"}
    self.tb_listen = appdf.initListener(events,handler(self,self.customEventHandler),self)
end

function ChangeHeadLayer:customEventHandler(event)
    if event.name == 'CHANGEFACE' then
        if event.data.result == 1 then
            showToast(self._scene,event.data.msg or "修改头像成功",1)
			
			GlobalUserItem.wFaceID = self.newfaceId
			self.oldfaceId = self.newfaceId
            self._scene:reSetHeadImg()
			self._scene._scene._head:updateHead(GlobalUserItem)
        end
		appdf.removeListener(self.tb_listen)
        self:removeFromParent()
    else
        showToast(self._scene,event.data.msg or "修改头像失败",1)
    end
end

function ChangeHeadLayer:loadCSB()
    local _,root = ExternalFun.loadRootCSB("userinfo/changeHeadImgLayer.csb",self)
    root = root:getChildByName("Panel_1")
    ExternalFun.FixTopUI(root)
	
    self.bg = root:getChildByName("registerBg")
    ExternalFun.playOpenLayerAnimation( self.bg )
    ExternalFun.FixScaleUI(self.bg,false,false,1/0.4)
    local bg = self.bg
    self.btn_close = bg:getChildByName("closebt")
    self.btn_sure = bg:getChildByName("btn_sure")

    local btnCallback = function(ref,type)
        if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
            self:onClicked(ref:getTag(),ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
        end
    end

    local strname = "headImg"
    for i = 1 , 10 do
        self["hImg" .. tostring(i)] = bg:getChildByName(strname .. tostring(i))
    end
    
    self.btn_close:addTouchEventListener(btnCallback)    
    self.btn_sure:addTouchEventListener(btnCallback)
end

function ChangeHeadLayer:resetImgs() 
    for i = 1, 10 do
        self["hImg" .. i]:setVisible(false)
    end

    local imgCallback = function(ref,type)
        if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect()
            self:onChangeImgClicked(ref:getTag(),ref)
        end
    end

    for i = 1, 10 do
		local imgname = string.format("Avatar%d.png", i-1)
       -- local img = ccui.ImageView:create(imgname,ccui.TextureResType.localType)
        self["hImg" .. i]:loadTexture(imgname,ccui.TextureResType.plistType)
        self["hImg" .. i]:setVisible(true)
        self["hImg" .. i]:setTag(i)
        self["hImg" .. i]:addTouchEventListener(imgCallback)
		 self["hImg" .. i]:setContentSize(cc.size(125,125))
    end
	self:onChangeImgClicked(GlobalUserItem.wFaceID+1,self["hImg" .. (GlobalUserItem.wFaceID+1)])
end

function ChangeHeadLayer:onChangeImgClicked(tag,sender)
    self.oldfaceId = GlobalUserItem.wFaceID

    self.newfaceId = tag - 1
	if self.selectImg and self.selectImg:getChildByName("selectsp") then
		self.selectImg:getChildByName("selectsp"):removeFromParent()
	end

	self.selectImg = sender
	local imgsize = self.selectImg:getContentSize()
	local selectsp = display.newSprite("userinfo/selectsp.png")
	selectsp:setAnchorPoint(cc.p(0.5,0.5))
	selectsp:setPosition(imgsize.width,imgsize.height)
	selectsp:setName("selectsp")
	self.selectImg:addChild(selectsp)
end

function ChangeHeadLayer:init()
    self:resetImgs()
end

function ChangeHeadLayer:onClicked(tag,ref)
    if ref == self.btn_close then
		for k,v in pairs(self.tb_listen) do
			cc.Director:getInstance():getEventDispatcher():removeEventListener(v)
		end
        self:removeFromParent()
    elseif ref == self.btn_sure then
        self._scene._scene._LogonFrame:sendChangeFaceId({faceid=self.newfaceId})
    end
end


return ChangeHeadLayer